package com.sqx.modules.labelType.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.goods.dao.GoodsDao;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.labelType.entity.LabelType;
import com.sqx.modules.labelType.service.LabelTypeService;
import io.swagger.annotations.ApiParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分类标签-管理端
 * @author wuChang
 * @since 2024-05-29
 */
@RestController
@Api(value = "分类标签-管理端", tags = {"分类标签-管理端"})
@RequestMapping("/admin/labelType/")
public class LabelTypeController {
    @Autowired
    private LabelTypeService labelTypeService;
    @Autowired
    private GoodsDao goodsDao;

    /**
     * 新增或修改分类标签
     *@param labelType 分类标签
     */
    @ApiOperation("新增或修改分类标签")
    @PostMapping(value = "saveLabelType")
    public Result saveLabelType(@ApiParam("分类标签")LabelType labelType) {
        return labelTypeService.saveLabelType(labelType);
    }

    /**
     * 获取分类标签列表
     * @param labelType 分类标签
     */
    @ApiOperation("获取分类标签列表")
    @GetMapping(value = "getLabelTypeList")
    public Result getLabelTypeList(@ApiParam("页码")Integer page,
                                   @ApiParam("条数")Integer limit,
                                   @ApiParam("分类标签")LabelType labelType) {
        return Result.success().put("data", labelTypeService.getLabelTypeList1(page, limit, labelType));
    }

    /**
     * 删除分类标签
     *@param labelId 分类标签id
     */
    @ApiOperation("删除分类标签")
    @PostMapping(value = "deleteLabelTypeById")
    public Result deleteLabelTypeById(@ApiParam("分类标签id")Long labelId) {
        List<Goods> goods = goodsDao.selectList(new QueryWrapper<Goods>().eq("label_id", labelId));
        if (!goods.isEmpty()) {
            return Result.error("该类型下有关联，请勿删除");
        }
        return labelTypeService.removeById(labelId) ? Result.success() : Result.error();
    }


    /**
     * 获取分类标签详情
     *@param labelId 分类标签id
     */
    @ApiOperation("获取分类标签详情")
    @GetMapping(value = "getLabelTypeById")
    public Result getLabelTypeById(@ApiParam("分类标签id")Long labelId) {
        return Result.success().put("data", labelTypeService.getById(labelId));
    }

}